package com.ww.learn.oop.sam

/**
 * 回调函数
 */

fun interface OnClickListener {
    fun onClick(view: String)
}

class Button {
    private var clickListener: OnClickListener? = null

    fun setOnClickListener(listener: OnClickListener) {
        clickListener = listener
    }

    fun click() {
        clickListener?.onClick("Button Clicked")
    }
}

// 打印结果
//Lambda: Button Clicked was clicked
//Button clicked: Button Clicked
fun main() {
    val button = Button()

    // 方式1：使用lambda
    button.setOnClickListener({ view ->
        println("Lambda: $view was clicked")
    })
    button.click()

    // 方式2：使用函数引用
    button.setOnClickListener(::handleButtonClick)

    button.click()
}

fun handleButtonClick(view: String) {
    println("Button clicked: $view")
}